package com.peternguyen.totolistpro;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import com.peternguyen.totolistpro.dao.CategoryDAO;
import com.peternguyen.totolistpro.dao.ConfigDAO;
import com.peternguyen.totolistpro.dao.TodoDAO;
import com.peternguyen.totolistpro.data.Category;
import com.peternguyen.totolistpro.data.Configapp;
import com.peternguyen.totolistpro.data.ToDo;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ToDoAdapter extends BaseAdapter {
	private Activity activity;
	private ArrayList<ToDo> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;
	TodoDAO dao;
	CategoryDAO catDao;
	ConfigDAO confDao;
	Category cat;
	Configapp conf;

	public ToDoAdapter(Activity a, ArrayList<ToDo> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		// imageLoader = new ImageLoader(activity.getApplicationContext());
		dao = new TodoDAO(a);
		catDao = new CategoryDAO(a);
		confDao = new ConfigDAO(a);
		conf = confDao.getConfig("numday_duedate");
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.row_task, null);

		TextView title = (TextView) vi.findViewById(R.id.title_todo); // title
		TextView artist = (TextView) vi.findViewById(R.id.lblTodo); // artist
		// name
		// name
		TextView duedate = (TextView) vi.findViewById(R.id.duedate); //
		ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image); // thumb

		ToDo todo = new ToDo();
		todo = data.get(position);
		// Setting all values in listview
		title.setText(todo.get_name());
		switch (todo.get_priority()) {
		case 1:
			// image
			thumb_image.setImageResource(R.drawable.check_box_4);
			break;
		case 2:
			// image
			thumb_image.setImageResource(R.drawable.check_box_2);
			break;
		case 3:
			// image
			thumb_image.setImageResource(R.drawable.check_box_1);
			break;

		default:
			// image
			thumb_image.setImageResource(R.drawable.check_box_4);
			break;
		}
		
		SimpleDateFormat formatterout = new SimpleDateFormat(
				"dd-MM-yyyy HH:mm");

		Calendar from = Calendar.getInstance();
		from.add(Calendar.DAY_OF_YEAR, Integer.parseInt("-" + conf.get_value()));
		// Date newfrom = from.getTime();

		Calendar to = Calendar.getInstance();
		to.add(Calendar.DAY_OF_YEAR, 0);
		Log.v("priority", "OK");
		Date dateTodo = todo.get_deadline_date();
	
		if(dateTodo != null){
			duedate.setText(formatterout.format(todo.get_deadline_date()));
			if (dateTodo.getTime() >= from.getTime().getTime()
					&& dateTodo.getTime() <= to.getTime().getTime()) {
				duedate.setTextColor(activity.getResources().getColor(
						R.color.importance_1));
			}
		}
		
		// catDao = new CategoryDAO(activity);
		Category _cat = new Category();
		_cat = catDao.getCategory(todo.get_catid());
		Log.v("priority", String.valueOf(todo.get_priority()));
		// Log.v("_Cat", String.valueOf(_cat.get_name()));
		if (_cat != null) {
			artist.setText(_cat.get_name());
		} else {
			artist.setText("");
		}
		/*
		 * if (cat.get_img() != null) { byte[] outImage = cat.get_img();
		 * ByteArrayInputStream imageStream = new ByteArrayInputStream(
		 * outImage); Bitmap theImage = BitmapFactory.decodeStream(imageStream);
		 * thumb_image.setImageBitmap(theImage); }else{
		 * 
		 * }
		 */
		// duration.setText(song.get(CategoryActivity.KEY_DURATION));
		// thumb_image.setImageBitmap(song.get(CategoryActivity.THUMB_URL));

		/*
		 * ImageButton btn = (ImageButton) vi.findViewById(R.id.btndelete);
		 * btn.setOnClickListener(new OnClickListener() {
		 * 
		 * @Override public void onClick(View v) { // TODO Auto-generated method
		 * stub
		 * 
		 * //onListItemClick(this, this, position, data.get(position), v); }
		 * 
		 * });
		 */

		return vi;
	}

}